package com.industrial.white.handler.excel;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.industrial.white.util.CommonUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ExcelMergeChooseFieldHandler implements RowWriteHandler {

    /**
     * 需要跳过的行索引
     */
    private Integer jumpRowIndex;

    List<String> titleList = new ArrayList<>();

    private Set<String> field;

    private int lastMergeColum = -1;

    private Row row1 = null;

    public ExcelMergeChooseFieldHandler(Integer jumpRowIndex,Set<String> field) {
        this.jumpRowIndex = jumpRowIndex;
        this.field = field;
        titleList.add("产品名称");
        titleList.add("规格型号");
        titleList.add("设计年产能");
        titleList.add("实际年产量");
        titleList.add("实际年销量");
        titleList.add("所属领域");
        titleList.add("关键供应商");
        titleList.add("关键用户");
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                Row row, Integer relativeRowIndex, Boolean isHead) {
        if(!(ArrayUtil.containsAny(CommonUtil.enterField.toArray(),field.toArray()) && ArrayUtil.containsAny(CommonUtil.proectField.toArray(),field.toArray()))){
            return;
        }
        if(row1 == null){
            //获取当前行的 统一社会信用代码
            Sheet sheet1 = writeSheetHolder.getSheet();
            row1 = sheet1.getRow(2);
        }

        int currentRowIndex = row.getRowNum();
        if(currentRowIndex > jumpRowIndex){
            mergeCellHandler(writeSheetHolder,row,currentRowIndex,row1);
        }
    }

    private void mergeCellHandler(WriteSheetHolder writeSheetHolder, Row row, int currentRowIndex,Row row1) {
        short currentRowLastCellNum = row1.getLastCellNum();
        short currentRowUsciIndex = -1;
        for(short i = 0; i < currentRowLastCellNum; i++){
            Cell cell = row1.getCell(i);
            String currentRowCellValue = cell.getStringCellValue();
            if(StringUtils.equals(currentRowCellValue,"统一社会信用代码")){
                currentRowUsciIndex = i;
                break;
            }
        }
        if(currentRowUsciIndex == -1){
            throw new RuntimeException("统一社会信用代码作为合并单元格的唯一标识，现在没有找到统一社会信用代码");
        }
        Cell currentRowUscc = row.getCell(currentRowUsciIndex);
        Object currentUscc = currentRowUscc.getCellType() == CellType.STRING ? currentRowUscc.getStringCellValue() : currentRowUscc.getNumericCellValue();
        //获取上一行的统一社会信用代码
        Cell preRowUscc = row.getSheet().getRow(currentRowIndex - 1).getCell(currentRowUsciIndex);
        Object preUscc = preRowUscc.getCellType() == CellType.STRING ? preRowUscc.getStringCellValue() : preRowUscc.getNumericCellValue();

        boolean pre = currentUscc.equals(preUscc);
        if(pre){

            Cell preRowXh = row.getSheet().getRow(currentRowIndex - 1).getCell(0);
            String preXh = preRowXh.getStringCellValue();

            row.getCell(0).setCellValue(preXh);

            Sheet sheet = writeSheetHolder.getSheet();
            if(lastMergeColum == -1){
                Row titleRow = sheet.getRow(2);
                short lastCellNum = titleRow.getLastCellNum();
                for(short i = 0;i < lastCellNum;i++){
                    Cell cell = titleRow.getCell(i);
                    String cellValue = cell.getStringCellValue();
                    if(ArrayUtil.contains(titleList.toArray(),cellValue)){
                        lastMergeColum = i;
                        break;
                    }
                }
            }
            if(lastMergeColum != -1){
                for(int i = 0; i <= lastMergeColum - 1; i++){
//                    CellRangeAddress cellAddresses = new CellRangeAddress(currentRowIndex - 1,currentRowIndex,i,i);
//                    sheet.addMergedRegion(cellAddresses);
                    boolean isMerge = false;
                    //获取到所有的合并的区域
                    List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
                    for(int j =0; j < mergedRegions.size() && !isMerge; j++){
                        CellRangeAddress cellAddresses = mergedRegions.get(j);
                        if(cellAddresses.isInRange(currentRowIndex - 1,i)){
                            sheet.removeMergedRegion(j);
                            cellAddresses.setLastRow(currentRowIndex);
                            sheet.addMergedRegion(cellAddresses);
                            isMerge = true;
                        }
                    }
                    if(!isMerge){
                        CellRangeAddress cellAddresses = new CellRangeAddress(currentRowIndex - 1, currentRowIndex, i, i);
                        sheet.addMergedRegion(cellAddresses);
                    }
                }
            }

        }else{
            //获取到上一行的序号
            Cell preRowXh = row.getSheet().getRow(currentRowIndex - 1).getCell(0);
            String preXh = preRowXh.getStringCellValue();
            if(StringUtils.isNotBlank(preXh)){
                Integer pp = Integer.valueOf(preXh) + 1;
                row.getCell(0).setCellValue(String.valueOf(pp));
            }
        }
    }
}
